﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class ManageMenu : System.Web.UI.UserControl
    {
        Menubar menu = new Menubar();
        DAL_Class.Menu me = new DAL_Class.Menu();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
            Panel1.Visible = false;
            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedRow.RowIndex < 0)
            {
                return;
            }
            Panel1.Visible = true;
            TxtMenu.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
            TxtSortNo.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
            if (((CheckBox)GridView1.SelectedRow.Cells[6].Controls[0]).Checked)
            { CbcStatu.Checked = true; }
            else { CbcStatu.Checked = false; }
          //  Ddlposi.SelectedItem.Text = GridView1.SelectedRow.Cells[4].Text;
            TxtMetatit.Text = HttpUtility.HtmlDecode(GridView1.Rows[GridView1.SelectedIndex].Cells[7].Text);
            TxtMetades.Text = ((Label)GridView1.SelectedRow.FindControl("Label1")).Text;
            TxtMetakey.Text = ((Label)GridView1.SelectedRow.FindControl("Label2")).Text;
            TxtMetaTo.Text = ((Label)GridView1.SelectedRow.FindControl("Label3")).Text;
            TxtMetaaut.Text = ((Label)GridView1.SelectedRow.FindControl("Label4")).Text;
            GridView1.DataBind();
        }
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            me = new DAL_Class.Menu();
            me.ID = Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Value);
            me.Name = TxtMenu.Text;
            me.ParrentID = 0;
            if (LbxMenuid.SelectedValue != "")
            {
                me.ParrentID = Convert.ToInt32(LbxMenuid.SelectedValue);
            }
            me.Position = Ddlposi.SelectedValue;
            if (CbcStatu.Checked) { me.Status = true; } else { me.Status = false; }
            me.SortNumber = Convert.ToInt32(TxtSortNo.Text);
            me.MetaTitle = TxtMetatit.Text;
            me.MetaDescription = TxtMetades.Text;
            me.MetaKeywords = TxtMetakey.Text;
            me.MetaRobots = TxtMetaTo.Text;
            me.MetaAuthor = TxtMetaaut.Text;
            LblCheck.Text = menu.UpdateMenu(me);
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TxtMenu.Text = string.Empty;
            TxtMetaaut.Text = string.Empty;
            TxtMetades.Text = string.Empty;
            TxtMetakey.Text = string.Empty;
            TxtMetatit.Text = string.Empty;
            TxtMetaTo.Text = string.Empty;
            TxtSortNo.Text = string.Empty;
            LblCheck.Text = string.Empty;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("Cbx");
                if (cb != null && cb.Checked)
                {
                    menu.DeleteMenu(Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value));
                }
            }
            GridView1.DataBind();
        }

        protected void CbxDele_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                
                if (CbxDele.Checked)
                {
                    ((CheckBox)GridView1.Rows[i].FindControl("Cbx")).Checked = true;
                }
                else
                {
                    ((CheckBox)GridView1.Rows[i].FindControl("Cbx")).Checked = false;
                }
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemMenu.aspx");
        }

        protected void Ddlposi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Ddlposi.SelectedValue == "Chủ Đề")
            {
                LbxMenuid.DataSourceID = "LinqDataSource3";
                LbxMenuid.DataBind();
            }
            else { LbxMenuid.Items.Clear(); }
        }

       

      
    }
}